﻿using UnityEngine;  
using UnityEngine.UI;
using DG.Tweening;
using System.Collections;
using System;  
using UnityEngine.SceneManagement;
using System.Threading;  
using System.Text;  
using System.Net;  
using System.Net.Sockets;  
using System.Collections.Generic;  
using System.IO;  
using System.Runtime.InteropServices;  
using System.Runtime.Serialization;  
using System.Runtime.Serialization.Formatters.Binary;  
using tools;
using evt;

public class Heart : MonoBehaviour {

	public static bool StopHeart = false;
	public static int TryCount = 0;
	public static int SendNum = 0;
	private bool few = false;
	Ping ping = null;
	// Use this for initialization
	void Start () {

	
		//InvokeRepeating ("getServerTime",3f,3f);
		InvokeRepeating ("PingRemoteServer",5f,5f);
		InvokeRepeating ("CheckFew",300f,300f);
		gameObject.name = "Heart";
	}

	private void CheckFew(){
		few = true;
	}

	private void PingRemoteServer(){
		string ResServerIP = "115.29.145.177"; 
		ping = new Ping(ResServerIP);
	}
	
	private void getServerTime(){

		if(StopHeart){

			if( GSocket.connectioned ){
				
				SendNum++;
				GSocket.Send( ByteFactory.makeByte(100150,null));
				if ( SendNum > 500 ) {
					SendNum = 0;
					Heart.StopHeart = false;
					GSocket.Closed ();

					if (ZjhView.inZJHGAME) {
						ServerEventDispather.getInstance ().removeAll ();
						SceneManager.LoadScene (2);
					}

					if (!ZJHAlert.Show) {
						
						if (TryCount > 5) {
							ServerEventDispather.getInstance ().Add (ServerEvent.ERROR_MSG, "当前网络或服务器异常，请重启游戏并检查网络");
							ZJHAlert.callback = ReConnection;
						} else {
							TryCount++;
							ServerEventDispather.getInstance ().Add (ServerEvent.ERROR_MSG, "网络连接已经断开,点击按钮重新连接");
							ZJHAlert.callback = ReConnection;
						}
					}
					/*
					SendNum = 0;
					if ( !ZJHAlert.Show ) {
						string warnStr = "当前与服务器连接不稳定  请检查你的网络连接";
						ServerEventDispather.getInstance ().Add (ServerEvent.ERROR_MSG, warnStr);
					}
					*/
				}
			}

			if (!GSocket.connectioned) {
				if (SceneManager.GetActiveScene ().buildIndex == 2) {
					GameObject.Find ("assyoufuck").transform.localPosition = Vector3.zero;
				}

				//GSocket.useThread = false;
				Debug.Log ("链接断开2=======================================================================================================================");
				//GSocket.Closed ();

				if (ZjhView.inZJHGAME) {
					ServerEventDispather.getInstance ().removeAll ();
					SceneManager.LoadScene (2);
				}

			
				ServerEventDispather.getInstance ().Add (ServerEvent.ERROR_MSG, "网络连接已经断开,点击按钮重新连接");
				ZJHAlert.callback = ReConnection;

			} else {
				if (SceneManager.GetActiveScene ().buildIndex == 2) {
					GameObject.Find ("assyoufuck").transform.localPosition = new Vector3(10000,10000,0);
				}
			}
	
		}
	}

	/*
	void OnApplicationPause(bool isPause)
    { 

        if( isPause)
        {
			if (GSocket.connectioned) {
				GSocket.useThread = false;
				Heart.StopHeart = false;
				GSocket.Closed ();
			}
            Debug.Log("后台化");

        }
		else
        {
            Debug.Log("回到游戏");
        }
    }
*/

	private void ReConnection(object flag){
		GSocket.reConnection = true;
		GSocket.StartSocket ();
	}



	
	// Update is called once per frame
	void Update () {
		/*
		if (frameIndex >= 5) {
			frameIndex = 0;
			if (GetNetworkReachabilityType () == 2) {
				string warnStr = "当前无网络 请检查您的网络设置";
				ServerEventDispather.getInstance ().Add (ServerEvent.ERROR_MSG, warnStr);
			} else {
				if ( GetNetworkReachabilityType () != UpNetWordType ) {
					string str1 = UpNetWordType == 0? "无线网络" : "移动网络";
					string str2 = GetNetworkReachabilityType () == 0 ?"无线网络" : "移动网络";
					string warnStr = "您的手机将网络设置从"+ str1 +"切换到了"+str2 +"这可能导致连接不稳定 请在稳定的网络下运行游戏";
					ServerEventDispather.getInstance ().Add (ServerEvent.ERROR_MSG, warnStr);
					UpNetWordType = GetNetworkReachabilityType ();
				}
			}
		}
		*/

		if(ping != null && ping.isDone) 
		{ 
			if(ping.time > 500){
				if ( few ) {
					few = false;
					if (GameObject.Find ("OfflineAlert") != null) {
						GameObject.Find ("OfflineAlert").transform.GetChild(0).GetChild(0).GetComponent<Text>().text = "当前与服务器连接不稳定  请检查你的网络连接";
						GameObject.Find ("OfflineAlert").transform.localPosition = Vector3.zero;
						Tweener tweener = GameObject.Find ("OfflineAlert").transform.DOLocalMoveY (300, 2f);
						tweener.onComplete = delegate {
							GameObject.Find ("OfflineAlert").transform.localPosition = new Vector3 (10000, 10000, 0);
						};

					}

				}

			}
			ping = null;
		} 
	}
}
